<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
	<body>
		<div id="usersToolbar">
			<div class="usersToolbar_button" style="height: 40px;">
				<a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="user_openAdd()" plain="true">添加</a>
	            <a href="#" class="easyui-linkbutton" iconCls="icon-edit" onclick="user_openEdit()" plain="true">修改</a>
	            <a href="#" class="easyui-linkbutton" iconCls="icon-remove" onclick="user_remove()" plain="true">删除</a>
	            <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="user_cancel()" plain="true">取消</a>
	            <a href="#" class="easyui-linkbutton" iconCls="icon-reload" onclick="user_reload()" plain="true">刷新</a>
			</div>
			<div style="height: 40px;">
				<label>用户名：</label><input class="easyui-textbox" id="user_searchbox" style="width:150px,height:24px;">
            	<a href="#" class="easyui-linkbutton" iconCls="icon-search" id="user_searchbtn">开始检索</a>
			</div>
		</div>
		
		
		<div id="user_dialog" class="easyui-dialog" data-options="closed:true,iconCls:'icon-save'" style="width: 400px; padding: 10px;">
			<form id="user_dialog_form" method="post">
				<table>
					<tr>
						<td width="100" align="right">用户名称：<input type="hidden" id="id" name="id" /> </td>
						<td><input type="text" id="username" name="username" class="easyui-textbox" data-options="required:true"/></td>
					</tr>
					<tr>
						<td width="100" align="right">密　　码：</td>
						<td><input type="text"  id="password" name="password" class="easyui-textbox" data-options="required:true" /></td>
					</tr>
					<tr>
						<td width="100" align="right">用户邮箱：</td>
						<td><input  type="text"  id="email" name="email" class="easyui-textbox" data-options="required:true" /></td>
					</tr>
					<tr>
					<tr>
						<td width="100" align="right">用户状态：</td>
						<td><input type="text"  id="state" name="state" class="easyui-numberbox" data-options="min:0,max:1,required:true,editable:true" /></td>
					</tr>
					<tr>
						<td width="100" align="right">注册日期：</td>
						<td><input type="text"  id="regDate" name="regDate" class="easyui-datebox" data-options="required:true" /></td>
					</tr>
					<tr>
						<td width="100" align="right">用户等级：</td>
						<td><input type="text"  id="roleId" name="role" class="easyui-combotree"
								data-options="url:'/filesharingsystem/roles_list',method:'get',required:true,panelHeight:133"/>
								</td>
					</tr>
				</table>
			</form>
		</div>
		<table id="users_da"></table>
		<script type="text/javascript">
			$(function(){
				$('#users_da').datagrid({
					url:pageContext_request_contextPath+'users',
					method:"get",
					pagination:true,
					fitColumns:true,
					border:false,
					pageSize:10,
					columns:[[
						{field:"ck",checkbox:true},
						{field:'id',title:'id',width:100},
						{field:'username',title:'username',width:100},
						{field:'email',title:'email',width:100,align:'right'},
						{field:"state",title:"用户状态",width:200,formatter:function(value,row,index){
							if(value == 1){
								return "正常状态";
							}else{
								return "禁用状态";
							}
						}},
						{field:'role',title:'role',width:100,formatter:function(value,row,index){
							return value.roleName;
						}},
						{field:'regDate',title:'reg_date',width:100}
					]]
				});
				
				
				$("#user_searchbtn").bind("click",function(){
					var queryParams = $("#users_da").datagrid("options").queryParams;
					queryParams.username = $("#user_searchbox").textbox("getValue");
					$("#users_da").datagrid("load");
				});
			});
			
			
			
			function user_openAdd(){
				$("#user_dialog_form").form("clear");
				$("#user_dialog").dialog({
					title:"添加用户",
					closed:false,
					modal:true,
					onOpen:function(){
						$("#email").textbox({disabled:false});	
					},
					buttons:[
						{
							text:"确定",iconCls:"icon-ok",
							handler:user_add
						},
						{
							text:"取消",iconCls:"icon-cancel",
							handler:function(){
								$("#user_dialog").dialog("close");
							}
						}
					],
				});
			}
			
			function user_openEdit() {
				$("#user_dialog_form").form("clear");
				var rows = $("#users_da").datagrid("getSelections");
				if(rows.length > 1){
					$.messager.alert("信息提示","一次只能修改一条数据！","info");
				}else if(rows.length == 0){
					$.messager.alert("信息提示","请勾选您要修改的数据！","info");
				}else{
					var user = rows[0];
					user.password = "";
					$("#user_dialog_form").form("load",user); // 选中的行的数据绑定到form表单上
					$("#user_dialog").dialog({
						title:"修改用户",
						closed:false,
						modal:true,
						onOpen:function(){
							$("#email").textbox({disabled:false});	
						},
						buttons:[
							{
								text:"确定",iconCls:"icon-ok",
								handler: user_edit
							},
							{
								text:"取消",iconCls:"icon-cancel",
								handler:function(){
									$("#user_dialog").dialog("close");
								}
							}
						]
					});
				}
			}
			
			
			function user_remove() {
				var rows = $("#users_da").datagrid("getSelections");
				if(rows.length < 1){
					$.messager.alert("信息提示","请勾选您要删除的数据！","info");
				}else{
					$.messager.confirm("信息提示","确定要删除选中的记录吗？",function(result){
						if(result){
							var ids = [];
							$(rows).each(function(){
								ids.push(this.id);
							});
							$.ajax({
								url:"/filesharingsystem/users",
								method:"delete",
								contentType: "application/json;charset=utf-8",
								data:JSON.stringify(ids),
								success:function(data){ //"ok","error"
										user_reload();
								},
								error:function(){
									$.messager.alert("信息提示","删除失败！");
								}
							});
						}
					});
				}
				
			}
			
			
			
			function user_add() {
				//alert($(#rolesId).value);
				doAjax("/filesharingsystem/user","post");
			}
			
			function user_edit(){
				doAjax("/filesharingsystem/user","put");
			}
			
			function doAjax(param,_method){
				/* [{id:$("#roleValueId").val(),roleName:$("#roleTextName").val()}] */
	
				var roleObj = '{"id":"'+ $("#roleId").val()+'","roleName":"","state":"","permissions":[]}';
				roleObj = eval("(" + roleObj + ")");
				$.ajax({
					url:param,
					method:_method,
					contentType:"application/json;charset=utf-8",
					data:JSON.stringify({
						id:$("#id").val(),
						username:$("#username").val(),
						password:$("#password").val(),
						email:$("#email").val(),
						state:$("#state").val(),
						regDate:$("#regDate").val(),
						role: roleObj
					}),
					success:function(data){ //"ok","error"
						//if(data == "ok"){
							$("#user_dialog").dialog("close");
							user_reload();
						//}
						// else{
						// 	$.messager.alert("信息提示","提交失败！","info");
						// }
					},
					error:function(){
						$.messager.alert("信息提示","提交失败！");
					}
				});
			}
			
			function user_cancel() {
				$("#users_da").datagrid("rejectChanges");
			}
			
			function user_reload() {
				$("#users_da").datagrid("reload");
			}
		</script>
	</body>
</html>



